﻿/* $RCSFile: AnnouncementsUtil.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/Announcements/AnnouncementsUtil.cs $
 * $log$
 * Revision 19 2010/08/09 09:43:45  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:24  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.UI.DataTypes;
using AddonNice.Configuration;
using AddonNice.Settings;
using AddonNice.Diagnostics;
using AddonNice.Settings.PageModules.Announcements;
using AddonNice.CRMWrapper;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.Announcements
{
    public class AStr
    {
        public const string DelayExpire                 =   "DELAYEXPIRE";
        public const string RepeatDirectionSetting      =   "REPEATDIRECTIONSETTING";
        public const string RepeatColumns               =   "REPEATCOLUMNS";
        public const string ShowBorder                  =   "SHOWBORDER";
        public const string SortField                   =   "SORTFIELD";
        public const string SortDirection               =   "SORTDIRECTION";

        public const string AllAnnouncements            =  "ANNOUNCEMENT_ALL_ANNOUNCEMENTS";
        public const string ScrollDirection             =  "ANNOUNCEMENT_SCROLLDIRECTION";
        public const string RotatorType                 =  "ANNOUNCEMENT_ROTATORTYPE";
        public const string AnimationType               =  "ANNOUNCEMENT_ANIMATIONTYPE";
        public const string ScrollDuration              =  "ANNOUNCEMENT_SCROLLDURATION";
        public const string FrameDuration               =  "ANNOUNCEMENT_FRAMEDURATION";
        public const string WrapFrames                  =  "ANNOUNCEMENT_WRAPFRAMES";
        public const string TextWidth                   =  "ANNOUNCEMENT_TEXTWIDTH";
        public const string TextHeight                  =  "ANNOUNCEMENT_TEXTHEIGHT";
        public const string ItemWidth                   =  "ANNOUNCEMENT_ITEMWIDTH";
        public const string ItemHeight                  =  "ANNOUNCEMENT_ITEMHEIGHT";
        public const string AddCRM                      =  "ANNOUNCEMENT_ADDCRM";
        public const string UseAnotherModuleID          =  "ANNOUNCEMENT_USE_ANOTHER_MODULEID";
        public const string DedicatedModuleID           =  "ANNOUNCEMENT_DEDICATED_MODULEID";
    }

    /// <summary>
    /// Utility class shared by all announcement modules
    /// </summary>
    public class AnnouncementsUtil
    {
        /// <summary>
        /// 
        /// </summary>
        public static DataTable MakeTb()
        {
            DataTable tb                        =   new DataTable();
	        tb.Columns.Add("ItemID",typeof(int));
	        tb.Columns.Add("ModuleID",typeof(int));
	        tb.Columns.Add("CrmID",typeof(Guid));
	        tb.Columns.Add("CreatedByUser",typeof(string));
	        tb.Columns.Add("CreatedDate",typeof(DateTime));
            tb.Columns.Add("Title",typeof(string));
	        tb.Columns.Add("MoreLink",typeof(string));
	        tb.Columns.Add("ExpireDate",typeof(DateTime));
	        tb.Columns.Add("Description",typeof(string));
            return tb;
        }

        public static void AddFromCRM(CRMWrapperBase Wrapper,ResetWrapperHandler fn, DataTable tb)
        {
            if ( tb.Columns.Count == 0 )
                tb              =   AnnouncementsUtil.MakeTb();
            if ( !tb.Columns.Contains("ModuleID") )
                tb.Columns.Add("ModuleID",typeof(int));
            if ( !tb.Columns.Contains("CrmID") )
    	        tb.Columns.Add("CrmID",typeof(Guid));

            PropertyDescriptorCollection _columnDescriptors     =   null;
            ADNDynAnnouncement.PrepareAnnouncements(Wrapper,fn,
                    delegate(InternalDynamicEntity de)
                    {
                        ADNDynAnnouncement ob                       =   new ADNDynAnnouncement();
                        ob.InitTemplate(Wrapper,de,_columnDescriptors,null);
                        _columnDescriptors                          =   ob.PropertyDescriptorCollection;
                        DataRow rw                                  =   tb.NewRow();
                        rw["ItemID"]                                =   -1;
                        rw["CrmID"]                                 =   ob.businessunitnewsarticleid;
                        rw["ModuleID"]                              =   -1;
                        rw["CreatedByUser"]                         =   ob.organizationName;
                        rw["CreatedDate"]                           =   ob.createdonLocalDate;
                        rw["Title"]                                 =   ob.articletitle;
                        rw["MoreLink"]                              =   ob.articleurl;
                        rw["ExpireDate"]                            =   ob.activeuntilLocalDate;
                        rw["Description"]                           =   ob.newsarticle;
                        tb.Rows.Add(rw);
                        return true;
                    }
                    );
        }

    }
}
